package com.example.covid19;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.http.HttpStatus;

import java.nio.charset.StandardCharsets;

public class TestApi {

    public static void main(String[] args) {
        String res = createQr("https://www.google.com", true);
        if (res == null) {
            System.out.println(-1);
        } else {
            System.out.println(res);
        }
    }

    public static String createQr(String target, boolean isUrlCode) {
        String url = "https://cli.im/Apis/QrCode/saveStatic";
        // 创建Http实例
        // 创建HttpPost实例
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpPost httpPost = new HttpPost(url);
            MultipartEntityBuilder builder = null;
            if (isUrlCode) {
                builder = createUrl(target);
            } else {
                builder = createText(target);
            }
            HttpEntity entity = builder.build();
            httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);

            if (response.getStatusLine().getStatusCode() == HttpStatus.OK.value()) {
                // 返回
                String res = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                return StringEscapeUtils.unescapeJava(res);
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static MultipartEntityBuilder createUrl(String target) {

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setCharset(StandardCharsets.UTF_8);

        builder.addTextBody("info", target);
        builder.addTextBody("url", target);
        builder.addTextBody("level", "H");
        builder.addTextBody("size", "500");
        builder.addTextBody("fgcolor", "#000000");
        builder.addTextBody("bggcolor", "#FFFFFF");
        builder.addTextBody("transparent", "false");
        builder.addTextBody("type", "url");
        builder.addTextBody("codetype", "qr");
        builder.addTextBody("rawinfo", target);
        return builder;
    }

    public static MultipartEntityBuilder createText(String target) {

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();
        builder.setCharset(StandardCharsets.UTF_8);

        builder.addTextBody("info", target);
        builder.addTextBody("content", target);
        builder.addTextBody("level", "H");
        builder.addTextBody("size", "500");
        builder.addTextBody("fgcolor", "#000000");
        builder.addTextBody("bggcolor", "#FFFFFF");
        builder.addTextBody("transparent", "false");
        builder.addTextBody("type", "text");
        builder.addTextBody("codetype", "qr");
        return builder;
    }
}
